
#include <stdlib.h>
#include <stdio.h>

#include "webrtc/modules/audio_processing/aecm/include/echo_control_mobile.h"

#define FRAMEINT16 160
#define FRAMEBYTE (160*2)

int main(int argc, char *argv[])
{
    FILE *fidnear = fopen(argv[1], "rb");
	fseek(fidnear, 0, SEEK_END); int d2 = ftell(fidnear);
    fseek(fidnear, 0, SEEK_SET); int d1 = ftell(fidnear);
    int nframes = (d2-d1)/FRAMEBYTE;
    printf("total frames = %d\n", nframes);

    FILE *fidfar = fopen(argv[2], "rb");
    FILE *fidaec = fopen(argv[3], "wb");
    short micbuf[FRAMEINT16] = {0};
    short refbuf[FRAMEINT16] = {0};
    short aecbuf[FRAMEINT16] = {0};

    void *aecmInst = WebRtcAecm_Create();
    WebRtcAecm_Init(aecmInst, 16000);

    AecmConfig config;
    config.cngMode = AecmTrue;
    config.echoMode = 4;
    WebRtcAecm_set_config(aecmInst, config);

    for (int n=0; n<nframes; n++) {
        fread(micbuf, sizeof(short), FRAMEINT16, fidnear);
        fread(refbuf, sizeof(short), FRAMEINT16, fidfar);
        WebRtcAecm_BufferFarend(aecmInst, refbuf, FRAMEINT16);
        WebRtcAecm_Process(aecmInst, micbuf, NULL, aecbuf, FRAMEINT16, 1);
        fwrite(aecbuf, sizeof(short), FRAMEINT16, fidaec);
    }

    WebRtcAecm_Free(aecmInst);
    fclose(fidnear); fclose(fidfar); fclose(fidaec);

    return 0;
}



